﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA.UserControls
{
    public partial class UcGeneralHistory : BaseUserControl
    {
        #region Properties
        public String ValidationErrorMessage
        {
            get { return GetValidationErrorMessage(); }
        }
        #endregion

        #region Starting Form
        public UcGeneralHistory()
        {
            InitializeComponent();
        }
        private void UcGeneralHistory_Load(object sender, EventArgs e)
        {
            HandleHoursDayState();
        }
        #endregion

        #region Events
        private void txtStartDate_ValueChanged(object sender, EventArgs e)
        {
            HandleHoursDayState();
        }
        private void txtEndDate_ValueChanged(object sender, EventArgs e)
        {
            HandleHoursDayState();
        }
        #endregion

        #region Methods
        public bool ValidateForm()
        {
            return !String.IsNullOrEmpty(txtName.Text) &&
                txtStartDate.Value != DateTime.MinValue &&
                txtEndDate.Value != DateTime.MinValue &&
                txtEndDate.Value.CompareTo(txtStartDate.Value) >= 0 &&
                (txtStartDate.Value.Date.CompareTo(txtEndDate.Value.Date) <= 0 ||
                (txtStartDate.Value.Date.CompareTo(txtEndDate.Value.Date) < 0 &&
                !String.IsNullOrEmpty(txtHoursDay.Text.Trim()) &&
                Convert.ToInt32(txtHoursDay.Text.Trim()) > 0));
        }
        private string GetValidationErrorMessage()
        {
            StringBuilder message = new StringBuilder();
            if (String.IsNullOrEmpty(txtName.Text))
                message.Append("\r\nInforme o nome do histórico");
            if (txtStartDate.Value == DateTime.MinValue)
                message.Append("\r\nInforme a data inicial");
            if (txtEndDate.Value == DateTime.MinValue)
                message.Append("\r\nInforme a data final");
            if (txtEndDate.Value.CompareTo(txtStartDate.Value) < 0)
                message.Append("\r\nA data inicial não pode ser maior que a data final");
            if (txtEndDate.Value.Date.CompareTo(txtStartDate.Value.Date) > 0 &&
                (String.IsNullOrEmpty(txtHoursDay.Text.Trim()) ||
                (!String.IsNullOrEmpty(txtHoursDay.Text.Trim()) &&
                Convert.ToInt32(txtHoursDay.Text.Trim()) <= 0)))
                message.Append("\r\nA quantidade de horas por dia deve ser informada para intervalo de datas");
            return message.ToString();
        }
        internal void CleanForm()
        {
            txtName.Text = String.Empty;
            txtDescription.Text = String.Empty;
            txtStartDate.Value = DateTime.Now;
            txtEndDate.Value = DateTime.Now;
            txtHoursDay.Text = "0";
        }
        internal void LoadData(BusinessEntity.TaskHistory history)
        {
            if (history != null)
            {
                txtName.Text = history.Name;
                txtStartDate.Value = history.StartDate;
                txtEndDate.Value = history.EndDate;
                txtDescription.Text = history.Description;
                txtHoursDay.Text = history.HoursDay != Int32.MinValue ? history.HoursDay.ToString() : "0";
                txtName.Focus();
            }
        }
        private void HandleHoursDayState()
        {
            txtHoursDay.Enabled = txtStartDate.Value.Date.CompareTo(txtEndDate.Value.Date) < 0;
            if (txtStartDate.Value.Date.CompareTo(txtEndDate.Value.Date) >= 0)
                txtHoursDay.Text = "0";
        }
        #endregion
    }
}
